package com.example.malltest0.controller;


import com.example.malltest0.Vo.FollowersVo;
import com.example.malltest0.common.api.CommonResult;
import com.example.malltest0.mbg.model.Followers;
import com.example.malltest0.service.FollowService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/follow")
@Api(tags = "FollowController", description = "关注库管理")
public class FollowController {

    @Autowired
    private FollowService followService;

    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation("添加库")
    public CommonResult addFollow(@RequestBody Followers followers){
        followService.addFollow(followers);
        return CommonResult.success(" ");
    }

    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    @ApiOperation("修改库")
    public CommonResult updateFollow(@RequestBody Followers followers){
        int count=followService.updateFollow(followers);
        if(count>0){
            return CommonResult.success(" ");
        }
        return CommonResult.failed();
    }

    @ResponseBody
    @RequestMapping(value = "/selectOne",method = RequestMethod.GET)
    @ApiOperation("根据id查询")
    public CommonResult<FollowersVo> select(@RequestParam Long id){
        FollowersVo followers=followService.selectById(id);
        if(followers!=null){
            return CommonResult.success(followers);
        }
        return CommonResult.failed();
    }

    @ResponseBody
    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    @ApiOperation("查询所有")
    public CommonResult<PageInfo<FollowersVo>> selectAll(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<FollowersVo> list=followService.selectAll();
        if(list!=null){
            //通过构造PageInfo对象获取分页信息，如当前页码，总页数，总条数
            PageInfo<FollowersVo> pageInfo = new PageInfo<FollowersVo>(list);
            return CommonResult.success(pageInfo);
        }
        return CommonResult.failed();
    }


    @ResponseBody
    @RequestMapping(value = "/selectByUid",method = RequestMethod.GET)
    @ApiOperation("根据用户id查询")
    public CommonResult<PageInfo<FollowersVo>> selectByUid(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,@RequestParam Long uid){
        PageHelper.startPage(pageNum, pageSize);
        List<FollowersVo> list=followService.selectByUid(uid);
        if(list!=null){
            PageInfo<FollowersVo> pageInfo = new PageInfo<FollowersVo>(list);
            return CommonResult.success(pageInfo);
        }
        return CommonResult.failed();
    }



}
